<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);

class manage extends admin{
	private $db;
	private $admin_db;
	public $site;
	public $admin_id;
	
	function __construct() {
		parent::__construct();
		pc_base::auto_load_func('global');
		$this->db = pc_base::load_model('experience_type_model');
		$this->admin_db = pc_base::load_model('admin_model');
		$this->site = get_siteid();//获取站点ID
		$this->admin_id = param::get_cookie('userid');//获取管理员ID
	}
	
	public function init() {
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$where = "";
		$where .= "siteid = ".$this->site." and status != 2";
		if ($_GET['experience']) {
			$keyword = $_GET['experience'];
			$where .= " and name like '%$keyword%'";
		}
		$experienceinfo = $this->db->listinfo($where,'id DESC',$page,10);
		$admin_model = pc_base::load_model('admin_model');
		$adminInfo = $admin_model->getRealnameInfo();
		foreach ($experienceinfo as $k=>$v){
			//获取用户管理员真实名称数据
			$experienceinfo[$k]['username'] = $adminInfo[$v['updated_by']];
		}
		$pages = $this->db->pages;
		pc_base::load_sys_class('format', '', 0);
		include $this->admin_tpl('experience_type_list');
	}
	
	public function add() {
		if (isset($_POST['dosubmit'])) {
			$info = isset($_POST['data']) ? $_POST['data'] : showmessage(L('illegal_parameters'),HTTP_REFERER);
			$data['name'] = trim($info['name']);
			$data['en_name'] = trim($info['en_name']);
			$data['parent_id'] = $info['experience_type_id'];
			$data['create_time'] = $data['update_time'] = date('Y-m-d H:i:s');
			$data['created_by'] = $data['updated_by'] = $this->admin_id;
			$data['siteid'] = $this->site;
			$data['status'] = 1;
			$result = $this->db->insert($data,true);
			if ($result) {
				//记录行为日志
				$log_content = '<span style="color:green;">添加履历类型</span>[履历类型id：'.$result.'，履历类型名称：'.$info['name'].']';
				$this->manage_admin_log($log_content);
				showmessage(L('add_experience_success'),'?m=experience_type&c=manage&a=init');
			} else {
				showmessage(L('add_experience_error'),HTTP_REFERER);
			}
		} else {
			$where = "";
			$where .= "status != 2 and siteid = ".$this->site;
			$experience_type = $this->db->select($where,'id,name,parent_id');//获取履历类型
			$experience_type = $this->get_tree($experience_type,0);
			$show_validator = true;
			pc_base::load_sys_class('format','',0);
			pc_base::load_sys_class('form','',0);
			include $this->admin_tpl('experience_type_add');
		}
	}
	
	public function edit() {
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message not exits'),HTTP_REFERER);
		$info = $this->db->get_one(array('id'=>$id));
		if (isset($_POST['dosubmit'])) {
			$up_info = isset($_POST['data']) ? $_POST['data'] : showmessage(L('illegal_parameters'),HTTP_REFERER);
			//检测修改履历类型名称是否重复
			$condition = "";
			$condition .= "siteid = ".$this->site." and id != ".$id." and name = '".$up_info['name']."' or en_name = '".$up_info['en_name']."'";
			$exist = $this->db->get_one($condition);
			if ($exist) {
				showmessage(L('experience_type_exist'),HTTP_REFERER);
			}
			$data['name'] = trim($up_info['name']);
			$data['en_name'] = trim($up_info['en_name']);
			$data['parent_id'] = $up_info['experience_type_id'];
			$data['update_time'] = date('Y-m-d H:i:s');
			$data['updated_by'] = $this->admin_id;
			$data['siteid'] = $this->site;
			$data['status'] = 1;
			$result = $this->db->update($data,array('id'=>$id));
			if ($result) {
				//记录行为日志
				$log_content = '<span style="color:blue;">修改履历类型</span>[履历类型id：'.$id.'，履历类型名称：'.$up_info['name'].']';
				$this->manage_admin_log($log_content);
				showmessage(L('edit_experience_success'),'?m=experience_type&c=manage&a=init');
			} else {
				showmessage(L('edit_experience_error'),HTTP_REFERER);
			}
		} else {
			$where = "";
			$where .= "status != 2 and siteid = ".$this->site;
			$experience_type = $this->db->select($where,'id,name,parent_id');//获取履历类型
			$experience_type = $this->get_tree($experience_type,0);
			$show_header = false;
			$show_validator = true;
			pc_base::load_sys_class('format','',0);
			pc_base::load_sys_class('form','',0);
			include $this->admin_tpl('experience_type_edit');
		}
	}
	
	public function del(){
		if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
			showmessage(L('message not exits'), HTTP_REFERER);
		}
		$id = $_REQUEST['id'];
		if (is_array($id)) {
			foreach ($id as $k => $v) {
				if (intval($v)) {
					$id[$k] = intval($v);
				} else {
					unset($id[$k]);
				}
			}
			$id = implode('\',\'', $id);
		}
		$result = $this->db->update(array(
				'status' => '2'
		), "id in ('$id') or parent_id in ('$id')");
		if ($result) {
			$del_id = explode('\',\'', $id);
			$new_id = implode('，', $del_id);
			//记录行为日志
			$log_content = '<span style="color:red;">删除履历类型</span>[履历类型id：'.$new_id.']';
			$this->manage_admin_log($log_content);
			showmessage(L('del_experience_success'), HTTP_REFERER);
		} else {
			showmessage(L('del_experience_error'), HTTP_REFERER);
		}
	}
	
	/**
	* 递归履历分类
	* @date: 2017年3月17日 下午3:32:22
	* @author: LIYANG
	*/
	public function get_tree($arr,$pid) {
		$tree = array();
		foreach ($arr as $k=>$v){					//每次都声明一个新数组用来放子元素  
			if ($v['parent_id'] == $pid) {			//匹配子记录 
				$v['child'] = manage::get_tree($arr,$v['id']);
				if($v['child'] == null){
					unset($v['child']);          //如果子元素为空则unset()进行删除，说明已经到该分支的最后一个元素了（可选）
				}
				$tree[] = $v;
			}
		}
		return $tree;
	}
	
	/**
	* 检测履历类型名称是否重复
	* @date: 2017年3月17日 下午3:39:49
	* @author: LIYANG
	*/
	public function experience_check() {
		$where = "";
		$keyword = trim($_GET['name']);
		$en_keyword = trim($_GET['en_name']);
		if (!empty($keyword)) {
			$where .= " name = '$keyword'";
			$result = $this->db->get_one($where);
			if ($result['name'] == $keyword) {
				exit('0');
			} else {
				exit('1');
			}
		} elseif (!empty($en_keyword)){
			$where .= " en_name = '$en_keyword'";
			$result = $this->db->get_one($where);
			if ($result['en_name'] == $en_keyword) {
				exit('0');
			} else {
				exit('1');
			}
		} else {
			exit('0');
		}
	}
	
}